﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using ueEngine.Framework.Configuration;
using ueEngine.Framework.Common;
using ueEngine.Biz.Core.AppService.Support;
using System.Text.RegularExpressions;
using ueEngine.Biz.Core.ExServer.WorkPool;
using ueEngine.Biz.Core.ExServer.WorkPool.WorkHandle.Email;
using ueEngine.Web.WebLogic.Model;

namespace ueEngine.Web
{
    /// <summary>
    /// ExchangeService 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。
    // [System.Web.Script.Services.ScriptService]
    public class ExchangeService : System.Web.Services.WebService
    {

        [WebMethod]
        public void DoPositionIndex(string token)
        {
            try
            {
                ExchangeServiceAuth(token);
            }
            catch (NotSupportedException e)
            {
                LogHelper.Write(CommonLogger.ExServer, LogLevel.Error, e);
                return;
            }
            catch (Exception e)
            {
                LogHelper.Write(CommonLogger.ExServer, LogLevel.Error, e);
            }
            ExchangeBiz.DoIndex();
        }

        [WebMethod]
        public void ClearCache(string token)
        {
            try
            {
                ExchangeServiceAuth(token);
            }
            catch (NotSupportedException e)
            {
                LogHelper.Write(CommonLogger.ExServer, LogLevel.Error, e);
                return;
            }
            catch (Exception e)
            {
                LogHelper.Write(CommonLogger.ExServer, LogLevel.Error, e);
            }
            ueEngine.Framework.Caching.ueCache.Clear();
        }

        [WebMethod]
        public InformationData AdvertisementPush(string token, List<string> receive, string title, string displayName, string html)
        {
            try
            {
                InformationData data = new InformationData()
                {
                    Fail = new List<SendFailMsg>()
                };
                ExchangeServiceAuth(token);
                foreach (string item in receive)
                {
                    if (String.IsNullOrEmpty(item))
                    {
                        continue;
                    }
                    if (!new Regex("^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$").IsMatch(item))
                    {
                        data.Fail.Add(new SendFailMsg()
                        {
                            receive = item,
                            message = "格式错误"
                        });
                    }
                    displayName = String.IsNullOrEmpty(displayName) ? "重庆大学生兼职网" : displayName;
                    WorkPool.Append<AdvertisementEmail>(WorkType.email_for_advertisement,
                        new AdvertisementEmail()
                        {
                            DisplayName = displayName,
                            Title = title,
                            Html=html,
                            Recipient = item
                        }, null);
                }
                return data;
            }
            catch (NotSupportedException e)
            {
                LogHelper.Write(CommonLogger.ExServer, LogLevel.Error, e);
                return null;
            }
            catch (Exception e)
            {
                LogHelper.Write(CommonLogger.ExServer, LogLevel.Error, e);
                return null;
            }
        }

        [WebMethod]
        public void RecruitmentExamined(string token,string Receive, string caption, int recruitId)
        {
            try
            {
                ExchangeServiceAuth(token);
                if(!new Regex("^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$").IsMatch(Receive))
                {
                    LogHelper.Write(CommonLogger.WebError, LogLevel.Trace, "RecruitmentExamined接口调用失败，参数Receive格式错误。参数为：" + Receive);
                    return;
                }
                WorkPool.Append<AutoPushMessage>(WorkType.email_for_recruitmentexamined,
                    new AutoPushMessage()
                    {
                        Param = new string[2] { caption, recruitId.ToString() },
                        Recipient = Receive
                    }, null);
            }
            catch (NotSupportedException e)
            {
                LogHelper.Write(CommonLogger.ExServer, LogLevel.Error, e);
                return;
            }
            catch (Exception e)
            {
                LogHelper.Write(CommonLogger.ExServer, LogLevel.Error, e);
            }
        }

        private void ExchangeServiceAuth(string token)
        {
            if (ConfigReader.ESConfig.Key.Token != HashAlgorithm.Encrypt(token, EncryptMode.sha1))
            {
                LogHelper.Write(CommonLogger.WebError, LogLevel.Info, "提供的token错误，token:" + token);
                throw new NotSupportedException("提供的token错误");
            }
        }
    }
}
